package com.vhly.remoteapi.yahoo.contact;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

import java.io.*;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: vhly[FR]
 * Date: 2010-12-5
 * Time: 15:55:15
 * Email: vhly@163.com
 */
public class Contact extends ContactObject {
    public boolean isConnection;
    public Vector<Field> fields;
    public Vector<Category> categories;

    public Contact() {
        fields = new Vector<Field>();
    }

    /**
     * Read object from ObjectInput
     *
     * @param din ObjectInput
     */
    @Override
    protected void readObject(DataInput din) throws IOException {
        super.readObject(din);
        isConnection = din.readBoolean();
        int size = din.readInt();
        if (size > 0) {
            Category ct;
            categories = new Vector<Category>();
            for (int i = 0; i < size; i++) {
                ct = new Category();
                ct.readObject(din);
                categories.add(ct);
            }
        }
        size = din.readInt();
        if (size > 0) {
            Field fd;
            for (int i = 0; i < size; i++) {
                fd = new Field();
                fd.readObject(din);
                fields.add(fd);
            }
        }
    }

    /**
     * Write object to ObjectOutput
     *
     * @param dout Output
     */
    @Override
    protected void writeObject(DataOutput dout) throws IOException {
        super.writeObject(dout);
        dout.writeBoolean(isConnection);

    }

    public void parse(Element item) {
        super.parse(item);

        NodeList lst = item.getElementsByTagName("isConnection");
        if (lst != null) {
            int len = lst.getLength();
            if (len > 0) {
                Element el = (Element) lst.item(0);
                NodeList cs = el.getChildNodes();
                if (cs != null && cs.getLength() > 0) {
                    Node nd = cs.item(0);
                    if (nd instanceof Text) {
                        Text txt = (Text) nd;
                        String t = txt.getNodeValue();
                        isConnection = "true".equals(t);
                    }
                }
            }
        }

        NodeList fds = item.getElementsByTagName("fields");
        if (fds != null) {
            int len = fds.getLength();
            Field fd;
            for (int i = 0; i < len; i++) {
                Element eit = (Element) fds.item(i);
                fd = new Field();
                fd.parse(eit);
                fields.add(fd);
            }
        }
    }

    public Field findNicknameField() {
        Field ret = null;
        ret = findField(FieldType.nickname);
        return ret;
    }

    public Field findNameField() {
        Field ret = null;
        ret = findField(FieldType.name);
        return ret;
    }

    public Field findNumberField() {
        Field ret = null;
        ret = findField(FieldType.phone);
        return ret;
    }

    public Field findEmailField() {
        Field ret = null;
        ret = findField(FieldType.email);
        return ret;
    }

    private Field findField(FieldType type) {
        Field ret = null;
        int size = fields.size();
        Field fd;
        for (int i = 0; i < size; i++) {
            fd = fields.get(i);
            if (fd.type == type) {
                ret = fd;
                break;
            }
        }
        return ret;
    }
}
